package btp.Calendars;

public class ISOCalendar {
	// The ISO Calendar is popular in sweden and other European countries.
	// It specifies the date by giving an ordinal day of the week and the "Calendar week" in a Gregorian year.
	// The new year is always on a monday between 29/12 and 4/1 and the end of the year is on a Sunday between 28/12 and 3/1.
	// Year is either 364 or 371 days long.
	
	//Epoch is same as Gregorian
	public static final int EPOCH = 1;
	
	int year;
	int week;
	int day;
	
	public long getRDFromCalendar(){
		return RataDie.nthDay(week, RataDie.SUNDAY, GregorianCalendar.getRDFromGregorian(year-1, GregorianCalendar.DECEMBER, 28)) + day;
	}
	
	public static ISOCalendar getCalendarFromRD(long rd){
		int approx = GregorianCalendar.getYearFromRD(rd-3);
		ISOCalendar iso = new ISOCalendar(approx+1, 1, 1);
		if(rd<iso.getRDFromCalendar())
			iso.setYear(iso.getYear()-1);
		int week = (int) (1+Math.floor((rd-iso.getRDFromCalendar())/7.0));
		iso.setWeek(week);
		iso.setDay(RataDie.dayOfWeek(rd));
		return iso;
	}

	public ISOCalendar(int year, int week, int day) {
		super();
		this.year = year;
		this.week = week;
		this.day = day;
	}
	
	//371 days in a year.
	public boolean isYearLong(){
		boolean begin = RataDie.dayOfWeek(GregorianCalendar.getRDFromGregorian(year, 1, 1))==RataDie.THURSDAY;
		boolean end = RataDie.dayOfWeek(GregorianCalendar.getRDFromGregorian(year, 12, 31))==RataDie.THURSDAY;
		return begin || end;
	}

	public final int getYear() {
		return year;
	}

	public final void setYear(int year) {
		this.year = year;
	}

	public final int getWeek() {
		return week;
	}

	public final void setWeek(int week) {
		this.week = week;
	}

	public final int getDay() {
		return day;
	}

	public final void setDay(int day) {
		this.day = day;
	}
	
	
}
